#include <Arduino.h>
#include <ALIBEEEP.h>
#include <string.h>
#include <EEPROM.h>


	ALIBEEEP::ALIBEEEP(){
		_setVersion();
		_sessionID = 0;
	}

	bool ALIBEEEP::_setVersion(){
		String _latestVer = VERSION;
		String _EEPROMVer = getVersion();
	
		if(_latestVer != _EEPROMVer){
			_writeBytesFromString(VER_START_BYTE,_latestVer);
			return true;
		} else{
			return false;
		}
	}

	String ALIBEEEP::getVersion(){
		return _readBytesToString(VER_START_BYTE,VER_NUM_BYTES);
	}

	bool ALIBEEEP::setUIDPWD(String oldUID, String oldPWD, String newUID, String newPWD){
		bool bFlag= false;
		String _defUID = DEFAULT_UID;
		String _defPWD = DEFAULT_PWD;

		//force uid/pwd reset for first time. To reset to default uid/pwd, set DEFAULT_UID and DEFAULT_PWD in .H file
		if(_defUID != "" && _defPWD != ""){
			//Serial.begin(9600);
			Serial.println("def uid [" + _defUID + "]");
			Serial.println("def pwd [" + _defPWD + "]");

			_writeBytesFromString(UID_START_BYTE, _defUID); 
			_writeBytesFromString(PWD_START_BYTE, _defPWD); 
			
			String _uid = _getUID(); Serial.println("uid ["+_uid+"]");
			String _pwd = _getPWD(); Serial.println("pwd ["+_pwd+"]");
			bFlag=true;
		} else if (oldUID != "" && oldPWD != "" && newUID !="" && newPWD !="") {
			//Serial.begin(9600);
			String _uid = _getUID(); Serial.println("old uid ["+_uid+"]");
			String _pwd = _getPWD(); Serial.println("old pwd ["+_pwd+"]");
		
			if(oldUID == _uid && oldPWD == _pwd) {
				if(_uid != newUID) { _writeBytesFromString(UID_START_BYTE, newUID); bFlag=true;}
				if(_pwd != newPWD) { _writeBytesFromString(PWD_START_BYTE, newPWD); bFlag=true;}

				String _newuid = _getUID(); Serial.println("new uid ["+_newuid+"]");
				String _newpwd = _getPWD(); Serial.println("new pwd ["+_newpwd+"]");
				return bFlag;
			}
		}
		return bFlag;
	}
	
	String ALIBEEEP::_getUID(){
		return _readBytesToString(UID_START_BYTE, UID_NUM_BYTES);
	}
	
	String ALIBEEEP::_getPWD(){
		return _readBytesToString(PWD_START_BYTE, PWD_NUM_BYTES);
	}

	String ALIBEEEP::_readBytesToString(int startByte, int numberOfBytes){
		String sVal = "";
		byte bVal;
		for (int i =startByte; i<=startByte+numberOfBytes; i++){
			bVal = EEPROM.read(i);
			sVal += (char) bVal;
		}
		return(sVal);
	}
	
	void ALIBEEEP::_writeBytesFromString(int startByte, String valueToWrite){
		int writeByteLocation ;
		for (int i = 0; i<=valueToWrite.length(); i++){
			writeByteLocation = startByte + i;
			EEPROM.write(writeByteLocation ,valueToWrite.charAt(i));
		}
	}

	unsigned long ALIBEEEP::logIn(String UID, String PWD){
		String _uid=_getUID();
		String _pwd=_getPWD();
		
		if (_sessionID !=0){
			logOut();
		}

		int _seed;
		if(_uid == UID && _pwd == PWD){
			while(_sessionID == 0){
				// use analogRead on pin 0 (not connected) to generate a random value and use that random value as seed to pass to 
				// randomSeed();
				_seed = analogRead(0);
				randomSeed(analogRead(0));
				// generate random value as session id
				_sessionID = random();
			}
		}
		return _sessionID;
	}

	unsigned long ALIBEEEP::logOut(){
		_sessionID = 0;
		return _sessionID;
	}
	
	unsigned long ALIBEEEP::getSessionID(){
		return _sessionID;
	}
